package com.market.sys.api.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.market.sys.common.Result;
import com.market.sys.dto.AdminSearchDto;
import com.market.sys.entity.Admin;
import com.market.sys.service.IAdminService;
import com.market.sys.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <b></b>
 */
@RestController
@RequestMapping("/user")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Integer id){
        adminService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/save")
    public Result<?> save(@RequestBody Admin admin){
        if (Objects.isNull(admin)){
            return Result.error("新增或修改信息不能为空！");
        }
        if (EmptyUtil.isNumberEmpty(admin.getId())){
            admin.setPassword("123456");
        }
        boolean result = adminService.saveOrUpdate(admin);
        if (result){
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    @PostMapping("/list")
    public Result<Map<String ,Object>> search(@RequestBody AdminSearchDto form){
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(form.getUsername()),Admin::getUsername,form.getUsername());
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(form.getPhone()),Admin::getPhone,form.getPhone());

        Page<Admin> page = adminService.page(new Page<>(form.getPageNo(), form.getPageSize()), lambdaQueryWrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody Admin admin){
        Map<String ,Object> map = new HashMap<>();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, admin.getUsername());
        Admin admin1 = adminService.getOne(wrapper);
        if (Objects.isNull(admin1) || !admin.getPassword().equals(admin1.getPassword())){
            return Result.error("用户名或密码不正确,请重新登录!");
        }
        map.put("token", UUID.randomUUID());
        return Result.success(map);
    }

    @GetMapping("/info")
    public Result<Map<String ,Object>> getUserInfo(@RequestParam("token") String token){
        Map<String ,Object> map = new HashMap<>();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,"admin");
        Admin admin = adminService.getOne(wrapper);
        map.put("name",admin.getUsername());
        map.put("avatar",admin.getAvatar());
        map.put("roles",new ArrayList<>());

        return Result.success(map);
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("token") String token){
        System.out.println("token = " + token);
        return Result.success();
    }
}
